﻿<%@ WebHandler Language="C#" Class="RbPhysicsAndChemicalTestHandler" %>

using System;
using System.Web;
using BusinessRule.DTO;
using System.Web.SessionState;
using DatabaseAccess.DBUtility;
using System.Data.OracleClient;
using System.Data;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using BusinessRule.Business;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;

/**
 * Description: 热表理化试样信息维护
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * create Date:2022-08-11
 */

public class RbPhysicsAndChemicalTestHandler : IHttpHandler, IRequiresSessionState
{
    CurrentUserInfo m_UserInfo;
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request["action"].ToString();
            m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
            switch (action)
            {
                case "getData":
                    getData(context);
                    break;
                case "insertData":
                    insertData(context);//添加
                    break;
                case "updateData":
                    updateData(context);//更新
                    break;
                case "deleteData":
                    deleteData(context);//删除
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            var result = new uMESProcessResult();
            result.Result = false;
            result.Message = ex.Message;
            context.Response.ContentType = "text/html";
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }

    //查询已维护数据
    void getData(HttpContext context)
    {
        if (m_UserInfo == null)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "当前未登录", DBTable = null }));
        }
        else
        {
            string sql = @"
SELECT rb.rbphysicsandchemicaltestid,rb.productname,rb.qty,rb.workflowstepname,rb.testitemname,rb.senddate,rb.estimatedate,rb.notes,
       e.fullname,rb.createdate
FROM rbphysicsandchemicaltest rb
LEFT JOIN employee e ON e.employeeid = rb.createemployeeid
WHERE rb.isuse = 0 
";
            if (context.Request["productname"] != null && context.Request["productname"].ToString() != "")
            {
                sql += string.Format(" AND rb.productname LIKE '%{0}%'", context.Request["productname"].ToString());
            }
            if (context.Request["workflowstep"] != null && context.Request["workflowstep"].ToString() != "")
            {
                sql += string.Format(" AND rb.workflowstepname LIKE '%{0}%'", context.Request["workflowstep"].ToString());
            }
            if (context.Request["testitemname"] != null && context.Request["testitemname"].ToString() != "")
            {
                sql += string.Format(" AND rb.testitemname LIKE '%{0}%'", context.Request["testitemname"].ToString());
            }
            if (context.Request["createEmployee"] != null && context.Request["createEmployee"].ToString() != "")
            {
                sql += string.Format(" AND e.fullname LIKE '%{0}%'", context.Request["createEmployee"].ToString());
            }
            if (context.Request["sendDateStart"] != null && context.Request["sendDateStart"].ToString() != "")
            {
                sql += string.Format(" AND rb.senddate >= to_date('{0}','yyyy-mm-dd')", context.Request["sendDateStart"].ToString());
            }
            if (context.Request["sendDateEnd"] != null && context.Request["sendDateEnd"].ToString() != "")
            {
                sql += string.Format(" AND rb.senddate <= to_date('{0}','yyyy-mm-dd')", context.Request["sendDateEnd"].ToString());
            }
            if (context.Request["estimateDateStart"] != null && context.Request["estimateDateStart"].ToString() != "")
            {
                sql += string.Format(" AND rb.estimatedate >= to_date('{0}','yyyy-mm-dd')", context.Request["estimateDateStart"].ToString());
            }
            if (context.Request["estimateDateEnd"] != null && context.Request["estimateDateEnd"].ToString() != "")
            {
                sql += string.Format(" AND rb.estimatedate <= to_date('{0}','yyyy-mm-dd')", context.Request["estimateDateEnd"].ToString());
            }
            if (context.Request["createDateStart"] != null && context.Request["createDateStart"].ToString() != "")
            {
                sql += string.Format(" AND rb.createdate >= to_date('{0} 00:00:00','yyyy-mm-dd hh24:mi:ss')", context.Request["createDateStart"].ToString());
            }
            if (context.Request["createDateEnd"] != null && context.Request["createDateEnd"].ToString() != "")
            {
                sql += string.Format(" AND rb.createdate <= to_date('{0} 23:59:59','yyyy-mm-dd hh24:mi:ss')", context.Request["createDateEnd"].ToString());
            }
            
            uMESPagingDataDTO paraPage = new uMESPagingDataDTO();
            paraPage.CurrentPageIndex = Convert.ToInt32(context.Request.Form["pageNumber"].ToString());//页码
            paraPage.PageSize = Convert.ToInt32(context.Request.Form["PageSize"].ToString());//每页页数
            paraPage.strSQL = sql;
            uMESProcessResult result = DatabaseHelper.RunProcedureForPage(paraPage);
            foreach (DataColumn col in result.DBTable.Columns)
            {
                col.ColumnName = col.ColumnName.ToLower();
            }
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }

    //保存
    void insertData(HttpContext context)
    {
        if (m_UserInfo == null)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "当前未登录", DBTable = null }));
        }
        else
        {
            DataTable dtPostData = JsonConvert.DeserializeObject<DataTable>(context.Request.Form["postData"].ToString());
            string createemployeeid = m_UserInfo.EmployeeID.ToString();
            string sql = "";
            foreach (DataRow dr in dtPostData.Rows)
            {
                sql = @"
INSERT INTO rbphysicsandchemicaltest(createemployeeid,estimatedate,notes,productname,qty,rbphysicsandchemicaltestid,senddate,testitemname,workflowstepname,createdate,isuse)
VALUES('{0}',to_date('{1}','yyyy/mm/dd'),'{2}','{3}','{4}','{5}',to_date('{6}','yyyy/mm/dd'),'{7}','{8}',SYSDATE,0)";
                sql = string.Format(sql,
                    createemployeeid,
                    dr["estimatedate"].ToString(),
                    dr["notes"].ToString(),
                    dr["productname"].ToString(),
                    dr["qty"].ToString(),
                    Guid.NewGuid().ToString(),
                    dr["senddate"].ToString(),
                    dr["testitemname"].ToString(),
                    dr["workflowstepname"].ToString());
                int insertResult = DatabaseHelper.ExecuteSql(sql);
            }
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = true, Message = "", DBTable = null }));
        }
    }

    //更新
    void updateData(HttpContext context)
    {
        if (m_UserInfo == null)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "当前未登录", DBTable = null }));
        }
        else
        {
            DataTable checkedRowsData = JsonConvert.DeserializeObject<DataTable>(context.Request.Form["checkedRowsData"].ToString());
            DataTable postData = JsonConvert.DeserializeObject<DataTable>(context.Request.Form["postData"].ToString());
            DataRow datarow = postData.Rows[0];
            string sql = "";
            foreach (DataRow dr in checkedRowsData.Rows)
            {
                sql = @"
UPDATE rbphysicsandchemicaltest t SET t.estimatedate = to_date('{0}','yyyy/mm/dd'),t.notes = '{1}',t.productname='{2}',t.qty = '{3}',t.senddate = to_date('{4}','yyyy/mm/dd'),
       t.testitemname='{5}',t.workflowstepname = '{6}' 
WHERE t.rbphysicsandchemicaltestid = '{7}'
";
                sql = string.Format(sql,
                    datarow["estimateDate"].ToString(),
                    datarow["notes"].ToString(),
                    datarow["productname"].ToString(),
                    datarow["qty"].ToString(),
                    datarow["senddate"].ToString(),
                    datarow["testitemname"].ToString(),
                    datarow["workflowstepname"].ToString(),
                    dr["rbphysicsandchemicaltestid"].ToString());
                int insertResult = DatabaseHelper.ExecuteSql(sql);
            }
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = true, Message = "", DBTable = null }));
        }
    }

    //删除
    void deleteData(HttpContext context)
    {
        if (m_UserInfo == null)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "当前未登录", DBTable = null }));
        }
        else
        {
            DataTable dtPostData = JsonConvert.DeserializeObject<DataTable>(context.Request.Form["willBeDeleteData"].ToString());
            string sql = "";
            foreach (DataRow dr in dtPostData.Rows)
            {
                sql = string.Format("DELETE rbphysicsandchemicaltest t WHERE t.rbphysicsandchemicaltestid = '{0}'", dr["rbphysicsandchemicaltestid"].ToString());
                int insertResult = DatabaseHelper.ExecuteSql(sql);
            }
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = true, Message = "", DBTable = null }));
        }
    }
}